package com.weetop.cfw.base.presenter.imp

import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.RegexUtils
import com.weetop.cfw.base.presenter.RegisterPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.RegisterView
import com.weetop.cfw.bean.GeneralCodeTableBean
import com.weetop.cfw.bean.RegisterBean
import com.weetop.cfw.bean.VerificationCodeBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.register.RegisterActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils
import com.weetop.cfw.utils.StringExtensionUtils

class RegisterPresenterImp(private var registerView: RegisterView? = null):RegisterPresenter {


    /**
     *  用户注册
     *  @param username 用户昵称
     *  @param phoneNumber 手机号
     *  @param verificationCode 验证码
     *  @param logInPassword 登录密码
     *  @param againPassword 确认登录密码
     *  @param 来源
     */
    override fun register(registerActivity: RegisterActivity, username: String, phoneNumber: String, verificationCode: String, logInPassword: String, againPassword: String,source:Int) {
        when {
            username.isEmpty() -> {
               registerView?.showNativeShortToast("请输入用户名")
            }
            username.length < 2 || username.length > 16 -> {
                registerView?.showNativeShortToast("用户名长度必须是2到16位")
            }
            !StringExtensionUtils.containNoEnglishChineseAndNumberic(username) -> {
                registerView?.showNativeShortToast("用户名不能包含非数字、非英文和非汉字")
            }
            phoneNumber.isEmpty() -> {
                registerView?.showNativeShortToast("请输入手机号码")
            }
            !RegexUtils.isMobileExact(phoneNumber) -> {
                registerView?.showNativeShortToast("请输入手机号码")
            }
            verificationCode.isEmpty() -> {
                registerView?.showNativeShortToast("请输入验证码")
            }
            logInPassword.isEmpty() -> {
                registerView?.showNativeShortToast("请输入登录密码")
            }
            againPassword.isEmpty() -> {
                registerView?.showNativeShortToast("请确认密码")
            }
            logInPassword != againPassword -> {
                registerView?.showNativeShortToast("两次密码输入不一致")
            }
            source == -1 -> {
                registerView?.showNativeShortToast("请选择来源")
            }
            else -> {
                registerActivity.showLoadingDialog(registerActivity,"正在注册，请稍等...")
                val queryMap = mapOf("username" to username,"userpwd" to logInPassword,"mobile" to phoneNumber,"Smscode" to verificationCode,"utype" to source)
                LogUtils.d("queryMap",queryMap)
                RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().registerUser(username,logInPassword,phoneNumber,verificationCode,source),registerActivity,object:RxJavaCallBack<RegisterBean>(){
                    override fun onSuccess(t: RegisterBean?) {
                     registerView?.registerSuccess(t?.message)
                        registerActivity.dismissLoadingDialog()
                    }

                    override fun onRequestError(t: RegisterBean?) {
                        super.onRequestError(t)
                        registerActivity.dismissLoadingDialog()
                    }

                    override fun onErrorResponse(throwable: Throwable?) {
                        super.onErrorResponse(throwable)
                        registerActivity.dismissLoadingDialog()
                    }
                })
            }
        }
    }

    /**
     * 获取验证码
     * @param mobile 手机号
     */
    override fun getVerificationCode(registerActivity: RegisterActivity,mobile: String) {
        when {
            mobile.isEmpty() -> {
                registerView?.showNativeShortToast("请输入手机号")
            }
            !RegexUtils.isMobileExact(mobile) -> {
                LogUtils.d("mobile",mobile)
                registerView?.showNativeShortToast("请输入合法手机号")
            }
            else -> {
                RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getVerificationCode(mobile),registerActivity,object:RxJavaCallBack<VerificationCodeBean>(){
                    override fun onSuccess(t: VerificationCodeBean?) {
                        LogUtils.d(t?.toString())
                        registerView?.getVerificationCodeSuccess(t)
                    }
                })
            }
        }
    }

    /**
     * 来源列表数据
     */
    override fun sourceList(registerActivity: RegisterActivity) {
        registerActivity.showLoadingDialog(registerActivity,"正在获取来源列表...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().generalCodeTable(1800),registerActivity,object:RxJavaCallBack<GeneralCodeTableBean>(){

            override fun onSuccess(t: GeneralCodeTableBean?) {
                registerActivity.dismissLoadingDialog()
                registerView?.sourceListGetSuccess(t)
            }

            override fun onRequestError(t: GeneralCodeTableBean?) {
                super.onRequestError(t)
                registerActivity.dismissLoadingDialog()
                registerView?.showNativeShortToast(t?.message)
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                registerActivity.dismissLoadingDialog()
            }

        })
    }

    override fun attachView(baseView: BaseView) {
        registerView = baseView as RegisterView
    }

    override fun detachView() {
        registerView = null
    }
}